package org.op.nav;

import java.awt.Component;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import org.op.menu.MenuActionController;
import org.op.util.MessageController;

public class NavigatorPopupMenuViewImpl implements NavigatorPopupMenuView, ActionListener {
	
	private JPopupMenu documentMenu;
	private JPopupMenu folderMenu;
	
	private MessageController messageController;
	private MenuActionController menuActionController;
	
	public void initMenus() {
		documentMenu = new JPopupMenu();
		addMenuItem(documentMenu, "open.node");
		addMenuItem(documentMenu, "move.wikiword");
		addMenuItem(documentMenu, "trash.wikiword");
		documentMenu.addSeparator();
		addMenuItem(documentMenu, "alias.add");
		addMenuItem(documentMenu, "alias.edit");

		folderMenu = new JPopupMenu();
		addMenuItem(folderMenu, "add.wikiword");
		addMenuItem(folderMenu, "folder.add");
		addMenuItem(folderMenu, "folder.trash");
		addMenuItem(folderMenu, "move.wikiword");
	}
	
	private void addMenuItem(JPopupMenu menu, String name) {
		//String label = name; // TODO - load this label form a resource
		String label = messageController.getLabel(name);
		
        JMenuItem menuitem = new JMenuItem(label);
        menuitem.addActionListener(this);
        menuitem.setName(name);
        menu.add(menuitem);		
	}

	public void showMenu(Component component, Point point, String nodeType) {
		if (documentMenu==null || folderMenu==null) {
			initMenus();
		}
		if (nodeType.equalsIgnoreCase(NODE_TYPE_DOCUMENT)) {
			documentMenu.show(component, point.x, point.y);
		}
		if (nodeType.equalsIgnoreCase(NODE_TYPE_FOLDER)) {
			folderMenu.show(component, point.x, point.y);
		}
	}


	public void actionPerformed(ActionEvent event) {
		String menuItemName = ((JMenuItem)event.getSource()).getName();
		menuActionController.doAction(menuItemName);
	}
	
	public MessageController getMessageController() {
		return messageController;
	}

	public void setMessageController(MessageController messageController) {
		this.messageController = messageController;
	}

	public MenuActionController getMenuActionController() {
		return menuActionController;
	}

	public void setMenuActionController(MenuActionController menuActionController) {
		this.menuActionController = menuActionController;
	}

}
